package gentle.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import gentle.base.SqlMapper;
import gentle.base.StringUtil;
import gentle.entity.*;
import gentle.mapper.CustomerAppointmentDao;
import gentle.mapper.CustomerMemberDao;
import gentle.service.CustomerMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CustomerMemberServiceImpl implements CustomerMemberService {

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    private CustomerMemberDao dao;

    @Override
    public List<MembersUser> selectCustomerMemberList(Page<MembersUser> page, SearchMembersUser searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        //查询条件 时间和门店
        if (StringUtil.isNotEmpty(searchInfo.getStartCTime())) {
            params.put("startCTime", searchInfo.getStartCTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCTime())) {
            params.put("endCTime", searchInfo.getEndCTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartUpdateTime())) {
            params.put("startUpdateTime", searchInfo.getStartUpdateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndUpdateTime())) {
            params.put("endUpdateTime", searchInfo.getEndUpdateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPhone())) {
            params.put("phone", searchInfo.getPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName()) && !"请选择".equals(searchInfo.getOutpatientName())) {
            String outpatientName = "";
            String outpatientName2[] = searchInfo.getOutpatientName().split(",");
            if (outpatientName2.length == 1) {
                outpatientName = outpatientName2[0];
            } else {
                for (int i = 0; i < outpatientName2.length; i++) {
                    if (i == (outpatientName2.length - 1)) {
                        outpatientName += "'" + outpatientName2[i];
                    } else if (i == 0) {
                        outpatientName += outpatientName2[i] + "'" + ",";
                    } else {
                        outpatientName += "'" + outpatientName2[i] + "'" + ",";
                    }
                }
            }
            params.put("outpatientName", " d.simplename in ('" + outpatientName + "')");
        }
        String tableHead = searchInfo.getAbbreviation();
        params.put("tableName", tableHead +"customer_info");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getcustomerMemberQueryPage(page, params);
    }

    @Override
    public int getQueryPagesMemberListCount(Page<MembersUser> page, SearchMembersUser searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        //查询条件 时间和门店
        if (StringUtil.isNotEmpty(searchInfo.getStartCTime())) {
            params.put("startCTime", searchInfo.getStartCTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCTime())) {
            params.put("endCTime", searchInfo.getEndCTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartUpdateTime())) {
            params.put("startUpdateTime", searchInfo.getStartUpdateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndUpdateTime())) {
            params.put("endUpdateTime", searchInfo.getEndUpdateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPhone())) {
            params.put("phone", searchInfo.getPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getOutpatientName()) && !"请选择".equals(searchInfo.getOutpatientName())) {
            String outpatientName = "";
            String outpatientName2[] = searchInfo.getOutpatientName().split(",");
            if (outpatientName2.length == 1) {
                outpatientName = outpatientName2[0];
            } else {
                for (int i = 0; i < outpatientName2.length; i++) {
                    if (i == (outpatientName2.length - 1)) {
                        outpatientName += "'" + outpatientName2[i];
                    } else if (i == 0) {
                        outpatientName += outpatientName2[i] + "'" + ",";
                    } else {
                        outpatientName += "'" + outpatientName2[i] + "'" + ",";
                    }
                }
            }
            params.put("outpatientName", " d.simplename in ('" + outpatientName + "')");
        }
        String tableHead = searchInfo.getAbbreviation();
        params.put("tableName", tableHead +"customer_info");
        return dao.getcustomerMemberQueryPageCount(page, params);
    }

}